'''
$LastChangedBy: csgillespie $
$LastChangedDate: 2007-06-06 11:08:06 +0000 (Wed, 06 Jun 2007) $
Some basic unittesting
Really need to expand this.
'''
import unittest
import ConfigParser
import os, sys
DIR = '/data/ncsg3/svn/pythonR/BasisTools/trunk/'
sys.path.insert(0, DIR )
from pysbml import pysbml
from pysbml.GlobalVariables.ErrorClasses import EditError


class TestBasisWs(unittest.TestCase):
	'''Tests the visualisation stuff'''
	def setUp(self):
		'''Make a list of the SBML files available'''
		self.p =  pysbml(sbml_file = DIR + '/test/LV2.xml')
	
	def TestConnectToBasis(self):
		c_to_b = self.p.connectToBasis(username='colin', password='colin')
		self.assertEqual(c_to_b.isLoggedIn(), True)
		self.assertEqual(c_to_b.logOut(), True)
		self.assertEqual(c_to_b.isLoggedIn(), False)
	
	def TestUserDetails(self):
		c_to_b = self.p.connectToBasis(username='colin', password='colin')
		ud = c_to_b.userDetails()
		#Test changing the name, email address
		ud['fullName'] = 'Fred'
		ud['emailAddress'] = 'Colin@colin'
		self.assertEqual(ud['fullName'], 'Fred')
		self.assertEqual(ud['emailAddress'], 'Colin@colin')
		ud['fullName'] = 'Colin Gillespie'
		ud['emailAddress'] = 'c.gillespie@ncl.ac.uk'
		
		#Test changing the password
		ud['password'] = 'password'
		c_to_b = self.p.connectToBasis(username='colin', password='password')
		ud = c_to_b.userDetails()
		ud['password'] = 'colin'
		self.p.connectToBasis(username='colin', password='colin')

	def testModel(self):
		c_to_b = self.p.connectToBasis(username='colin', password='colin')
		old_sbml = self.p.sbml
		model = c_to_b.model()
		
		#Test saving the model
		model.saveModel()
		new_urn = self.p.model_urn
		self.failIf(not new_urn, 'Model urn not set')
		print model['modelName'], model
		sys.exit()
		#Get the model again
		model.getModel(new_urn)
		self.assertEqual(c_to_b.model_urn, new_urn)
		print model['modelName']
		model['modelName'] = 'FredName'
		model['modelNotes'] = 'FredNotes'
		print model['modelName']
		#Get the model again
		model['modelUrn'] = new_urn
		print model['modelName']
		name = model['modelName'].split(' - ')[0]
		#self.assertEqual(name, 'FredName')
		print name,model.model_urn
		
		self.assertEqual(model['modelNotes'], 'FredNotes')
			
		#Delete the model
		model.deleteModel()
		self.failIf(c_to_b.model_urn, 'Model urn should be None')
	

	def TestModelTable(self):
		c_to_b = self.p.connectToBasis(username='colin', password='colin')
		mt = c_to_b.modelTable()
		print mt.getMyModelInfo()
		
		print mt.getPublicModelInfo()
	
	def TestSimulate(self):
		c_to_b = self.p.connectToBasis(username='colin', password='colin')
		old_sim_group_urn = c_to_b.simulation_group_urn
		#save a model
		model = c_to_b.model()
		#Test saving the model
		model.saveModel()
		
		#Simulate a model
		sim = c_to_b.simulate()
		sim.simulate('r1', 100, 101, 1000)
		#Make sure we have a sim urn
		self.assertNotEqual(c_to_b.simulation_group_urn, old_sim_group_urn)
			
		self.assertEqual(sim.getListOfSpecies(), ['time', 'X', 'Y'])
		#Kill a simulation
		c_to_b.simulate().deleteSimulation()
		self.assertEqual(c_to_b.simulation_group_urn, None)
	
	
		
		
if __name__ == '__main__':
    unittest.main()
